package com.fadadianqi.logistics.sell.service;

import com.fadadianqi.commons.PrimaryKeyCreator;
import com.fadadianqi.logistics.customer.model.Account;
import com.fadadianqi.logistics.customer.model.Customer;
import com.fadadianqi.logistics.customer.service.AccountService;
import com.fadadianqi.logistics.customer.service.CustomerService;
import com.fadadianqi.logistics.sell.dao.PaymentDAO;
import com.fadadianqi.logistics.sell.model.Bill;
import com.fadadianqi.logistics.sell.model.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class PaymentService {

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Throwable.class)
    public void payment(Bill bill, boolean useBalance, Double cash) {
        double paid = paymentDAO.sumPaid(bill.getId()); // 已经支付过的
        double i = bill.getRealPayables(); // 应付总金额
        Customer customer = customerService.getById(bill.getCustomerId());
        if (customer == null) {
            Payment p = new Payment();
            p.setAddTime(new Date());
            p.setBillId(bill.getId());
            p.setId(PrimaryKeyCreator.create());
            p.setMoney(cash);
            paymentDAO.create(p);
            if (cash - (i - paid) >= 0) {
                bill.setIsChecked(Boolean.TRUE);
                billService.checked(bill);
            }
            return;
        }
        double money = 0;
        // 期初余额
        Double balance = accountService.getBalance(customer);
        if (cash != null && cash.doubleValue() != 0) {
            Account a = new Account();
            a.setAddTime(new Date());
            a.setBalance(balance + cash);
            a.setCustomerId(customer.getId());
            a.setMoney(cash);
            a.setId(PrimaryKeyCreator.create());
            a.setUniqueId(a.getId());
            a.setBillId(bill.getId());
            accountService.create(a);
            if (useBalance) {
                money = balance + cash;
            } else {
                money = cash;
            }
        } else {
            if (useBalance) {
                money = balance;
            }
        }
        double z = (i - paid); // 需要支付的金额
        double rz = 0D; // 实际支出的金额
        if (z > money) {
            // 余额不足
            rz = money;
        } else {
            rz = z;
            bill.setIsChecked(Boolean.TRUE);
            billService.checked(bill);
        }
        if (rz == 0) {
            return;
        }
        customer.setDebt(customer.getDebt().doubleValue() + rz);
        //
        Payment p = new Payment();
        p.setAddTime(new Date());
        p.setBillId(bill.getId());
        p.setId(PrimaryKeyCreator.create());
        p.setMoney(rz);
        paymentDAO.create(p);
        //
        Account a = new Account();
        a.setAddTime(new Date());
        a.setBalance(balance - rz);
        a.setCustomerId(customer.getId());
        a.setMoney(-rz);
        a.setId(PrimaryKeyCreator.create());
        a.setUniqueId(a.getId());
        a.setBillId(bill.getId());
        accountService.create(a);
    }

    /**
     * 获取指定账单的已付款数目
     *
     * @param bill
     * @return
     */
    public double sumPaid(Bill bill) {
        return paymentDAO.sumPaid(bill.getId());
    }

    public List<Payment> findByBillId(Bill bill) {
        if (bill == null) {
            return null;
        }
        return paymentDAO.findByBillId(bill.getId());
    }

    public void remove(Payment p) {
        paymentDAO.remove(p);
    }

    @Autowired
    private AccountService accountService;

    @Autowired
    private PaymentDAO paymentDAO;

    @Autowired
    private BillService billService;

    @Autowired
    private CustomerService customerService;

}
